/*
 * The variable to hold the lines/commands
 */
var lines;

/*
 * The current line number
 */
var line_no = 0;

/*
 * Each command is an action. The delay between each action is 100ms
 */
var actionDelay = 100;

/*
 * Pause the interpret() function by setting this to 1. Restarting it is just 
 * calling it again.
 */
var interpret_pause = 0;

/*
 * The main function which is called on submitting the code
 */
function execute(txt)
{
	lines = null;
	line_no = 0;
	lines = txt.split("\n");
	document.getElementById('mytext').style.backgroundColor = 'rgb(50,100,50)'; // highlight the background as green, assuming that the code is correct
	interpret();
}

/*
 * The monster interpreter
 */
function interpret()
{
	if (line_no >= lines.length)
		return;
	
	var error = 1; // we assume that there is error by default
	var errorString = null; // the reason for the error
	var token = (lines[line_no]).tokenize(" ", ",", true);

	// if it is an empty line, continue to next line without any delay
	if (token.length == 0) {
		error = 0;
		line_no++;
		setTimeout(interpret, 0);
		return;
	}

	if(token[0] == "repeat")
	{
		// Currently the loop syntax is tight. 
		// Easing it means that we have to parse word by word 
		// instead of line by line. Right syntax:
		// repeat <times>
		// {
		//	..
		// }
		// TODO: Parse word by word instead of line by line.
		if(token.length == 2)
		{
			error = 0;
			line_no++;

			var loop_count = parseInt(token[1]);
			var count1=0;
			var count2=0;
			var li = line_no;

			var loop_depth = 0;
			var current_depth = 0;

			while(li<lines.length)
			{
				var open_bracket = lines[li].indexOf('{');
				var close_bracket = lines[li].indexOf('}');

				if(open_bracket != -1 && count1 == 0) 
				{
					count1 = li;

				}
				else if(open_bracket != -1)
				{
					loop_depth++;
				}   
				else if(close_bracket != -1 && loop_depth == current_depth)
				{
					count2 = li;

				}						
				else if(close_bracket!=-1)
					current_depth++;

				if(count1!=0 && count2!=0)
					break;
				li++;
			}
			if(loop_depth!=current_depth || count2 == 0)
			{
				errorString = "The number of brackets don't match";
				error = 1;
			}
			interpret_repeat(count1, count2, loop_count);
		}
		else
		{
			errorString = "Loop sytax wrong. Right syntax: repeat <times>\n{\n..\n}";
		}
	}

	if(token[0] == "canvascolor")
	{
		if(token.length == 4)
		{
			line_no++;
			error = 0;

			var l=parseInt(token[1]);
			var m=parseInt(token[2]);
			var n=parseInt(token[3]);
			canvascolor(l, m, n);
		}
		else
		{
			errorString = "Not sufficient arguments";
			// setTimeout(interpret,500);
		}
	}

	if(token[0]=="pencolor")
	{
		if(token.length == 4)
		{

			line_no++;
			error = 0;

			var l=parseInt(token[1]);
			var m=parseInt(token[2]);
			var n=parseInt(token[3]);					
			pencolor(l, m, n);
		}
		else
		{
			// Do we ignore errors?
			setTimeout(interpret,500);
		}
	}

	if(token[0]=="penwidth")
	{
		if(token.length == 2)
		{
			line_no++;
			error = 0;

			var l=parseInt(token[1]);
			penwidth(l);
		}
		else
		{
			// Do we ignore errors?
			setTimeout(interpret,500);
		}
	}

	if(token[0]=="canvassize")
	{
		if(token.length == 3)
		{
			line_no++;
			error = 0;

			var l=parseInt(token[1]);
			var m = parseInt(token[2]);
			canvassize(l, m);
		}
		else
		{
			// Do we ignore errors?
			setTimeout(interpret,500);
		}
	}

	if(token[0] == "go")
	{
		if(token.length == 3)
		{
			line_no++;
			error = 0;

			var l=parseInt(token[1]);
			var m = parseInt(token[2]);
			go(l, m);
		}
		else
		{
			// Do we ignore errors?
			setTimeout(interpret,500);
		}
	}	

	if(token[0]=="gox")
	{
		if(token.length == 2)
		{
			line_no++;
			error = 0;

			var l=parseInt(token[1]);
			gox(l);
		}
		else
		{
			// Do we ignore errors?
			setTimeout(interpret,500);
		}

	}

	if(token[0] == "goy")
	{
		if(token.length == 2)
		{
			line_no++;
			error = 0;

			var l=parseInt(token[1]);
			goy(l);
		}
		else
		{
			// Do we ignore errors?
			setTimeout(interpret,500);
		}
	}

	if(token[0] == "getx")
	{

		if(token.length == 1)
		{

			line_no++;
			error = 0;
			getx();
		}
		else
		{
			// Do we ignore errors?
			setTimeout(interpret,500);
		}
	}

	if(token[0] == "gety")
	{
		return y;
	}


	if(token[0] == "forward")
	{	
		if(token.length == 2)
		{
			line_no++;
			error = 0;

			var l=parseInt(token[1]);
			forward(l);
		}
		else
		{
			// Do we ignore errors?
			setTimeout(interpret,500);
		}
	}

	if(token[0] == "backward")
	{
		if(token.length == 2)
		{
			line_no++;
			error = 0;

			var l=parseInt(token[1]);
			backward(l);
		}
		else
		{
			// Do we ignore errors?
			setTimeout(interpret,500);
		}
	}

	if(token[0] =="turnleft")
	{
		if(token.length == 2)
		{
			line_no++;
			error = 0;

			var l=parseInt(token[1]);
			turnleft(l);
		}
		else
		{
			// Do we ignore errors?
			setTimeout(interpret,500);
		}
	}

	if(token[0] =="turnright")
	{
		if(token.length == 2)
		{
			line_no++;
			error = 0;

			var l=parseInt(token[1]);
			turnright(l);
		}
		else
		{
			// Do we ignore errors?
			setTimeout(interpret,500);
		}

	}

	if(token[0] == "direction")
	{
		if(token.length == 2)
		{
			line_no++;
			error = 0;

			var l=parseInt(token[1]);
			direction(l);
		}
		else
		{
			// Do we ignore errors?
			setTimeout(interpret,500);
		}

	}

	if(token[0] == "center")
	{
		if(token.length == 1)
		{
			line_no++;
			error = 0;

			center();
		}
		else
		{
			// Do we ignore errors?
			setTimeout(interpret,500);
		}
	}

	if(token[0] =="top")
	{

		if(token.length == 2)
		{
			line_no++;
			error = 0;

			var l=parseInt(token[1]);
			top(l);
		}

		else
		{
			// Do we ignore errors?
			setTimeout(interpret,500);
		}
	}

	if(token[0] =="down")
	{

		if(token.length == 2)
		{
			line_no++;
			error = 0;

			var l=parseInt(token[1]);
			down(l);
		}
		else
		{
			// Do we ignore errors?
			setTimeout(interpret,500);
		}

	}

	if(token[0]=="left")
	{
		if(token.length == 2)
		{
			line_no++;
			error = 0;

			var l=parseInt(token[1]);
			left(l);
		}
		else
		{
			// Do we ignore errors?
			setTimeout(interpret,500);
		}


	}

	if(token[0]=="right")
	{

		if(token.length == 2)
		{
			line_no++;
			error = 0;

			var l=parseInt(token[1]);
			right(l);
		}
		else
		{
			// Do we ignore errors?
			setTimeout(interpret,500);
		}

	}

	if(token[0] == "clear")
	{
		if(token.length == 1)
		{
			line_no++;
			error = 0;
			clear();
		}
		else
		{
			// Do we ignore errors?
			setTimeout(interpret,500);
		}
	}

	if (error == 1)
		document.getElementById('mytext').style.backgroundColor = 'rgb(90,0,0)';
	else if (interpret_pause == 0)
				setTimeout(interpret, actionDelay);
}

function interpret_repeat(x,y,l)
{
	lines[x] = lines[x].replace("{","");
	lines[y] = lines[y].replace("}","");

	var i=1;
	var line_count = y;
	var k;

	while(i<l)
	{
		k = x + 1;
		while(k < y)
		{
			lines.splice(line_count,0,lines[k]);
			k++;
			line_count++;
		}
		i++;
	}
}

